//
//  mb_SoundBuffer.c
//  MBAudio
//
//  Created by avansc on 4/18/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include <stdlib.h>
#include <stdio.h>

#include "MBAudio.h"

#ifdef __cplusplus
extern "C" {
#endif
    extern mb_pUIMap_t *mb_init_pUIMap();
    extern uint32_t mb_get_pUIMap(mb_pUIMap_t *h, const char *key);
    extern uint32_t mb_put_pUIMap(mb_pUIMap_t *h, const char *key, int *ret);
#ifdef __cplusplus
}
#endif
    
static mb_SoundBufferMap_t *g_SoundBufferMap = NULL;

static void initSoundBufferMap(void);
static int setSoundBufferMap(const char* key, unsigned int *val);
static unsigned int *getSoundBufferMap(const char* key);
static int addSoundBuffer(unsigned int buf);
static int removeSoundBuffer(unsigned int buf);

mb_SoundBufferMap_t *createSoundBufferMap(void)
{
    mb_SoundBufferMap_t *map = (mb_SoundBufferMap_t*)malloc(sizeof(mb_SoundBufferMap_t));
    
    map->m_initialized = 0;
    
    map->init = &initSoundBufferMap;
    map->set = &setSoundBufferMap;
    map->get = &getSoundBufferMap;
    map->add = &addSoundBuffer;
    map->remove = &removeSoundBuffer;
    
    if(!g_SoundBufferMap)
    {
        setActiveSoundBufferMap(map);
        initSoundBufferMap();
    }
    
    return map;
}

mb_SoundBufferMap_t *setActiveSoundBufferMap(mb_SoundBufferMap_t *map)
{
    mb_SoundBufferMap_t *temp = g_SoundBufferMap;
    g_SoundBufferMap = map;
    return temp;
}

void initSoundBufferMap(void)
{
    if (g_SoundBufferMap->m_initialized == 1)
        return;
    g_SoundBufferMap->m_initialized = 1;
    g_SoundBufferMap->m_buffersMap = mb_init_pUIMap();
}

int setSoundBufferMap(const char* key, unsigned int *val)
{
    int ret;
    uint32_t k = mb_put_pUIMap(g_SoundBufferMap->m_buffersMap, key, &ret);
    (g_SoundBufferMap->m_buffersMap)->vals[k] = val;
    
    return k;
}

unsigned int *getSoundBufferMap(const char* key)
{
    return (g_SoundBufferMap->m_buffersMap)->vals[mb_get_pUIMap(g_SoundBufferMap->m_buffersMap, key)];
}

int addSoundBuffer(unsigned int buf)
{
    // TODO
    return 0;
}

int removeSoundBuffer(unsigned int buf)
{
    // TODO
    return 0;
}